package com.cos.listener;

import com.cos.vo.Goods;
import com.cos.mq.MQ;
import com.cos.service.GoodsService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class ESListener {
    @Autowired
    private GoodsService goodsService;
    @RabbitListener(queues = MQ.GOODS_ES_QUEUE)
    public void add(Goods goodsEntity, Channel channel, Message message) throws IOException {
        System.out.println(goodsEntity);
        try {
            goodsService.addGoods(goodsEntity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            log.error("添加数据到es出现了异常!!!!!!!!!!!!!!!!!!!!!!",e);
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);

        }
    }

}
